package com.lanren.familybookapp.ui.main.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;

import com.hjq.toast.ToastUtils;
import com.lanren.common.app.ActivityStackManager;
import com.lanren.common.base.IPresenter;
import com.lanren.common.utils.AppUtils;
import com.lanren.common.utils.DoubleClickHelper;
import com.lanren.common.widget.navigation.view.EasyNavigationBar;
import com.lanren.easydialog.AnimatorHelper;
import com.lanren.familybookapp.R;
import com.lanren.familybookapp.base.BaseUiActivity;
import com.lanren.familybookapp.helper.upgrade.AppUpgradeHelp;
import com.lanren.familybookapp.helper.upgrade.IAppUpgradeView;
import com.lanren.familybookapp.ui.home.view.HomeFragment;
import com.lanren.familybookapp.ui.me.view.MeFragment;
import com.lanren.familybookapp.ui.table.view.TableFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @ClassName MainActivity
 * @Description TODO 主界面
 * @Author chongheng.wang
 * @Date 2019/10/3 13:18
 * @Version 1.0
 */
public class MainActivity extends BaseUiActivity implements IAppUpgradeView.AppUpgradeView {

    @BindView(R.id.bv_home_navigation)
    EasyNavigationBar bvHomeNavigation;

    private String[] tabText;
    //未选中icon
    private int[] normalIcon;
    //选中时icon
    private int[] selectIcon;


    @Override
    public IPresenter createPresenter() {
        return null;
    }


    @Override
    public int getLayoutResId() {
        return R.layout.activity_main;
    }


    @Override
    public void doCreate(Bundle savedInstanceState) {
        initView();
        initData();
    }

    public void initView() {
    }

    public void initData() {
        setNavigationData();

        List<Fragment> lazyFragments = new ArrayList<>();
        lazyFragments.add(HomeFragment.newInstance());
        lazyFragments.add(TableFragment.newInstance());
        lazyFragments.add(MeFragment.newInstance());

        bvHomeNavigation.titleItems(tabText)
                .normalIconItems(normalIcon)
                .selectIconItems(selectIcon)
                .mode(EasyNavigationBar.MODE_NORMAL)
                .normalTextColor(getResources().getColor(R.color.black40))
                .selectTextColor(getResources().getColor(R.color.black80))
                .fragmentList(lazyFragments)
                .fragmentManager(getSupportFragmentManager())
                .anim(AnimatorHelper.Landing)
                .canScroll(false)
                .build();

        AppUpgradeHelp.getInstance().checkVersion(this, AppUtils.getVersionName(), this);

    }


    @Override
    public void initEvent() {

    }


    @Override
    public void showError(int code, String msg) {
        if (code != 10004) {
            super.showError(code, msg);
        }
    }

    private void setNavigationData() {
        tabText = new String[]{"首页", "统计", "我的"};
        normalIcon = new int[]{R.drawable.icon_home_off, R.drawable.icon_table_off, R.drawable.icon_me_off};
        selectIcon = new int[]{R.drawable.icon_home_on, R.drawable.icon_table_on, R.drawable.icon_me_on};
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (DoubleClickHelper.isOnDoubleClick()) {
                // 进行内存优化，销毁掉所有的界面
                ActivityStackManager.getInstance().finishAllActivities();
                // 销毁进程
                System.exit(0);
            } else {
                ToastUtils.show(getResources().getString(R.string.home_exit_hint));
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
